package com.example.zzl.model.bean.SearchFragment;

import android.util.Log;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.example.zzl.app.MyApplication;
import com.example.zzl.view.activity.SearchActivity.AllActivity;
import com.google.gson.Gson;

import java.util.ArrayList;

/**
 * Created by zhang on 2017/2/10.
 */

public class LoadAllBrand {
    private Data data;

    public LoadAllBrand(Data data,String url) {
        this.data = data;
        Log.e("data","LoadAllBrand");
        loadData(url);
    }

    private void loadData(String url){
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                ArrayList allBrand = new Gson().fromJson(response,ArrayList.class);
                ArrayList<AllBrandData> allData = new ArrayList<>();
                for (int i = 0; i < allBrand.size(); i++) {
                    AllBrandData all = new Gson().fromJson(new Gson().toJson(allBrand.get(i)),AllBrandData.class);
                    allData.add(all);
                }
                data.sendAllBrand(allData);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("data",error.toString());
            }
        });
        request.setRetryPolicy( new DefaultRetryPolicy( 10000,//默认超时时间，应设置一个稍微大点儿的，例如本处的500000
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,//默认最大尝试次数
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT ) );
        MyApplication.queue.add(request);
    }
}
